home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part2 / 17663 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.3 KB

  1. Path: news.cyberport.com!usenet
  2. From: tangent@cyberport.com (Warren Young)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: STL: tiny vectors
  5. Date: Wed, 17 Apr 1996 00:20:43 GMT
  6. Organization: none
  7. Message-ID: <317435d2.535547356@news.cyberport.com>
  8. References: <doug-1304962242380001@port4.lightlink.com>
  9. NNTP-Posting-Host: ppp4.cyberport.com
  10. X-Newsreader: Forte Agent .99d/32.182
  11.  
  12. doug@lightlink.com (Doug Wyatt) wrote:
  13.  
  14. >Many of my vectors are rather small, 2-10 small elements.  The
  15. >implementation of allocator (HP via Metrowerks CodeWarrior) implements
  16. >memory in chunks of no less than 4096 bytes.  When I have many arrays
  17. >under 100 bytes, this adds up to a lot of wasted memory.
  18. >
  19. >I started writing code to make my algorithms multi-pass so I can know in
  20. >advance how large a vector is to be, and call reserve(), but it seems this
  21. >really shouldn't be necessary.
  22.  
  23. As I recall, HP STL will only allocate as much space as it needs if
  24. you use the ctor with an integer size parameter (I think it's just
  25. vector(int)).  Again, though, this requires that you know something
  26. about your data ahead of time.
  27.  
  28. The thing is, though, the allocation policies are up to the container,
  29. not a part of the standard at all.  So, you should be able to change
  30. the implementation in this regard and still have a "legal" STL.
  31.  
  32. >Another thing I've toyed with is using my own allocator which uses
  33. >malloc() and free() instead of the global operators new and delete.  That
  34. >way I can use realloc() to shrink a vector.  I suppose this will work but
  35.  
  36. Be careful with this, because this can move the data around in memory.
  37. So, you can only do it when the vector would have been modified
  38. anyway.  Consider what would happen if the user had some iterators
  39. into the vector and you realloc'd it.
  40.  
  41. Another potential problem: when you realloc memory, the data is moved
  42. with a bitwise copy.  This can cause real trouble, especially with
  43. contained classes that depend on their operator= or copy ctor getting
  44. called when they are moved.  Dangling pointers is just the beginning.
  45. Don't get me wrong...this will often work correctly, such as with
  46. built-in types and those classes that can successfully work with the
  47. copy ctor implicitly generated by the compiler when you don't supply
  48. one.
  49.  
  50. Anyway, you _can_ reallocate memory with new and delete, and in the
  51. same fashion, it's just not as clean: new the new block, copy the data
  52. over, and delete the old block.  realloc() does the same thing (or at
  53. least, it can), it's just a single call instead of three.
  54.  
  55. >I have a book that claims the default allocator should be using a page
  56. >size of 512, so perhaps making my own allocator use 512 would be a simple
  57.  
  58. Again, the STL's pre-allocation policy isn't spec'd in the standard.
  59. Would this happen to be Nelson's STL book?  If so, note that he only
  60. deals with vanilla HP STL in the book, so he can reasonably talk about
  61. such implementation details.
  62.  
  63. >Should I be using another container besides vector?  I thought about using
  64. >a list but I'd like to be able to retain fast random access into my
  65. >containers.
  66.  
  67. If I were you, I'd hack a vector<T> to do what you want it to.  If you
  68. stick with the same class interface, you should even be able to drop
  69. it in with minimal changes.  I'd change the container's name, though.
  70.  
  71. Or, find someone else's dynamic vector implementation that you like
  72. better, if STL compatibility isn't too big an issue for you.
  73.  
  74. = Warren --
  75.